/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.common.widget.display;


import androidx.annotation.Nullable;

import java.io.Closeable;
import java.io.IOException;
import java.util.Locale;

/**
 * @author Young Ken
 */

public class LangHelper {
    /**
     * 获取数值的位数，例如9返回1，99返回2，999返回3
     *
     * @param number 要计算位数的数值，必须>0
     * @return 数值的位数，若传的参数小于等于0，则返回0
     */
    public static int getNumberDigits(int number) {
        if (number <= 0) {
            return 0;
        }
        return (int) (Math.log10(number) + 1);
    }

    /**
     * get number digits
     *
     * @param number long
     * @return int
     */
    public static int getNumberDigits(long number) {
        if (number <= 0) {
            return 0;
        }
        return (int) (Math.log10(number) + 1);
    }

    /**
     * 规范化价格字符串显示的工具类
     *
     * @param price 价格
     * @return 保留两位小数的价格字符串
     */
    public static String regularizePrice(float price) {
        return String.format(Locale.CHINESE, "%.2f", price);
    }

    /**
     * 规范化价格字符串显示的工具类
     *
     * @param price 价格
     * @return 保留两位小数的价格字符串
     */
    public static String regularizePrice(double price) {
        return String.format(Locale.CHINESE, "%.2f", price);
    }

    /**
     * isNullOrEmpty
     *
     * @param string CharSequence
     * @return string
     */
    public static boolean isNullOrEmpty(@Nullable CharSequence string) {
        return string == null || string.length() == 0;
    }

    /**
     * clone
     *
     * @param closeable Closeable
     */
    public static void close(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * object Equals
     *
     * @param objectA a
     * @param objectB b
     * @return boolean
     */
    public static boolean objectEquals(Object objectA, Object objectB) {
        return (objectA == objectB) || (objectA != null && objectA.equals(objectB));
    }

    /**
     * constrain
     *
     * @param amount amount
     * @param low    low
     * @param high   high
     * @return float
     */
    public static int constrain(int amount, int low, int high) {
        return amount < low ? low : (amount > high ? high : amount);
    }

    /**
     * constrain
     *
     * @param amount amount
     * @param low    low
     * @param high   high
     * @return float
     */
    public static float constrain(float amount, float low, float high) {
        return amount < low ? low : (amount > high ? high : amount);
    }
}
